import DataBase from './Data/DataBase';
import StorageCenter from './StorageCenter';
import GameConfig from './Data/GameConfig';
import { StarType } from './Data/GameConfig';
import SoundUtil from '../Fantasy/Util/SoundUtil';
import LoadUtil from '../Fantasy/Util/LoadUtil';

export default class ServerCenter {

    /**
     * 获取某本书某单元信息
     * @param bookId 
     * @param unit 
     */
    public static getBookInfo(bookId: number, unit: number) {
        for (let info of DataBase.BookInfo) {
            if (info.bookId == bookId) {
                let unitArray = info.unitList;
                let index = Math.ceil(unit/2)-1;
                if (index >= unitArray.length){
                    Log('查询书本数组越界,出现错误',bookId,'单元:',unit)
                    return null;
                }
                return unitArray[index];
            }
        }
    }
        /**
     * 获取某本书名字
     * @param bookId 
     * @param unit 
     */
    public static getBookName(bookId: number) {
        for (let info of DataBase.BookInfo) {
            if (info.bookId == bookId) {
                return info.bookName;
            }
        }
    }
    /**
     * 获取体力
     */
    public static getUserStamina(){
        let num = StorageCenter.getUserStamina();
        return num;
    }
    /**
     * 获取用户经验值
     */
    public static getUserExp(){
        let exp = StorageCenter.getUserExp();
        return exp;
    }
    /**
     * 获取用户排名
     */
    public static getUserRank() {
        let exp = StorageCenter.getUserExp();
        let scoreArray = new Array(DataBase.RankInfo.length);
        for (let i = 0; i < scoreArray.length; i++) {
            scoreArray[i] = DataBase.RankInfo[i].score;
        }
        scoreArray.sort((a, b) => {
            return b - a;
        })
        for (let i = 0; i < scoreArray.length; i++) {
            if (exp >= scoreArray[i]) {
                return i + 1;
            }
        }
        return scoreArray.length + 1;
    }
    /**
     * 获取玩家PK胜利场次
     */
    public static getUserVictory (){
        let num = StorageCenter.getUserVictory();
        return num;
    }
    /**
     * 获取用户连续登陆几天
     */
    public static getUserContinueLogin(){
        let num = StorageCenter.getUserContinueLogin();
        return num;
    }
    /**
     * 获取排行榜信息
     * 返回结果 obj :{'rank':1,'list':排行榜用户信息数组};
     */
    public static getRankInfo() {
        let myInfo = StorageCenter.getUserInfo();
        let exp = myInfo.score;
        let RankArray = JSON.parse(JSON.stringify(DataBase.RankInfo));
        RankArray.sort((a, b) => {
            return b.score - a.score;
        })
        let newArray = [];
        let myRank = null;
        for (let i = 0; i < RankArray.length; i++) {
            if (exp >= RankArray[i].score && myRank == null) {
                myRank = i + 1;
                if (newArray.length < 100){
                    newArray.push(myInfo);
                }
            }
            else{
                if (newArray.length < 100){
                    newArray.push(RankArray[i]);
                }
            }
            if (myRank && newArray.length>=100){
                break;
            }
        }
        if (myRank == null){
            myRank = RankArray.length + 1;
        }
        let result = {
            'rank' : myRank,
            'list' : newArray,
        }
        return result;
    }

    /**
     * 获取当前书本 玩家处在第几关
     * @param bookId 
     */
    public static getCurrLevel(bookId){
        let max = this.getMaxLevel(bookId);
        for(let i=1;i<=max;i++){
            let info = StorageCenter.getUserRushLevel(i,bookId);
            if (info.easy && info.middle && info.high){

            }
            else{
                return i;
            }
        }
        return max;
    }

    /**
     * 获取当前书本最大关卡
     * @param bookId 
     */
    public static getMaxLevel(bookId){
        if(bookId >= 5 && bookId <= 12){
            if (bookId == 11){
                return 8;
            }
            else{
                return 12;
            }
        }
        else{
            Log('输入了错误的书本号');
        }
    }

    /**
     * 刷新经验值
     * @param expNum 当前经验值增加expnum
     */
    public static addUserExp(expNum){
        StorageCenter.addUserExp(expNum);
    }

     /**
     * 刷新体力值
     * @param manualNum 当前体力值加这个值,闯关扣除5点就填-5
     */
    public static addUserStamina(manualNum){
        StorageCenter.addUserStamina(manualNum);
    }

    /**
     * 获得卡片
     * @param card 
     */
    public static GetCard(card : StarType){
        let myCard = StorageCenter.getUserStar();
        let prop = GameConfig.getStarEnglish(card);
        myCard[prop] = (myCard[prop]+1) > 4 ? 4 : (myCard[prop]+1);
        StorageCenter.setUserStar(myCard);
    }
    /**
     * 获取星座卡片信息
     */
    public static getUserStar(){
        let myCard = StorageCenter.getUserStar();
        return myCard;
    }
    /**
     * 获取当前关卡的3个小怪通关情况
     * @param bookId 书本id
     * @param level 关卡id
     */
    public static getUnitPassInfo(bookId,level){
        let info =  StorageCenter.getUserRushLevel(level,bookId);    
        return info;
    }
    /**
     * 通关某书某单元某小关
     * @param level  第几单元
     * @param small     初露锋芒
     * @param bookId    第几本书
     */
    public static setUserRushLevel(level : number,small : number,bookId:number){
        StorageCenter.setUserRushLevel(level,small,bookId);
    }
    /**
     * 通关某书某单元某小关
     * @param boookId 书本id
     * @param unit     单元
     * @param word    单词
     */
    public static getWordVoice(boookId,unit,word){
        unit = Math.ceil(unit / 2);
        
        let adress = "wordVoice/"+boookId+"/"+unit+"/"+word;
        Log("音频地址");
        SoundUtil.playWordVoice(adress);
    }

        /**
     * 通关某书某单元某小关
     * @param boookId 书本id
     * @param unit     单元
     * @param container  cc.Sprite
     * @param word    单词
     */
    public static getWordImg(boookId,unit,container,word){
        unit = Math.ceil(unit / 2);
        let adress = "Texture/wordImg/"+boookId+"/"+unit+"/"+word;
        Log("图片地址");
        LoadUtil.LoadResImg(container,adress);
    }
}
